#!/bin/sh
# PCP QA Test No. 364
# pmlogconf metric accessibility
#
# Copyright (c) 2010 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

# see if unix domain sockets are available (permissions)
_get_libpcp_config
target="-h localhost"
$unix_domain_sockets && target="-h unix:"

status=0	# success is the default!
$sudo rm -rf $tmp.* $seq.full
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

_filter_solaris()
{
    if [ $PCP_PLATFORM != solaris ]
    then
	cat
    else
	sed \
	    -e '/^disk.dev.avactive .* Unknown metric name/d' \
	    -e '/^network.icmp.* Unknown metric name/d' \
	    -e '/^network.igmp.* Unknown metric name/d' \
	    -e '/^network.ip.* Unknown metric name/d' \
	    -e '/^network.tcp.* Unknown metric name/d' \
	    -e '/^network.udp.* Unknown metric name/d' \
	    -e '/^filesys.* Unknown metric name/d' \
	    -e '/^swap .* Unknown metric name/d' \
	    -e '/^swap.pages.* Unknown metric name/d' \
	    -e '/^swapdev .* Unknown metric name/d' \
	# solaris
    fi
}

_filter_darwin()
{
    if [ $PCP_PLATFORM != darwin ]
    then
	cat
    else
	sed \
	    -e '/^disk\.dev\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.all\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.partitions\..* Unknown metric name/d' \
	    -e '/^hinv\.nnode .* Unknown metric name/d' \
	    -e '/^network\.icmp .* Unknown metric name/d' \
	    -e '/^network\.igmp .* Unknown metric name/d' \
	    -e '/^network\.ip .* Unknown metric name/d' \
	    -e '/^network\.tcp .* Unknown metric name/d' \
	    -e '/^network\.udp .* Unknown metric name/d' \
	    -e '/^network\.udplite .* Unknown metric name/d' \
	    -e '/^swap .* Unknown metric name/d' \
	    -e '/^swap\..* .* Unknown metric name/d' \
	    -e '/^swapdev .* Unknown metric name/d' \
	    -e '/^proc\..* Unknown metric name/d' \
	    -e '/^kernel\.all\.intr .* Unknown metric name/d' \
	    -e '/^kernel\.all\.nusers .* Unknown metric name/d' \
	    -e '/^mem\.util\.bufmem .* Unknown metric name/d' \
	    -e '/^mem\.util\.cached .* Unknown metric name/d' \
	# darwin
    fi
}

_filter_freebsd()
{
    if [ $PCP_PLATFORM != freebsd ]
    then
	cat
    else
	sed \
	    -e '/^disk\.all\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.dev\.avactive .* Unknown metric name/d' \
	    -e '/^filesys .* Unknown metric name/d' \
	    -e '/^filesys\..* Unknown metric name/d' \
	    -e '/^hinv\.nnode .* Unknown metric name/d' \
	    -e '/^kernel\.all\.io\..* Unknown metric name/d' \
	    -e '/^kernel\.all\.kpreempt .* Unknown metric name/d' \
	    -e '/^kernel\.all\.kswitch .* Unknown metric name/d' \
	    -e '/^kernel\.all\.nusers .* Unknown metric name/d' \
	    -e '/^kernel\.all\.pswitch .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysexec .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysfork .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysioctl .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysread .* Unknown metric name/d' \
	    -e '/^kernel\.all\.syswrite .* Unknown metric name/d' \
	    -e '/^kernel\.all\.uptime .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.pswitch .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.syscall .* Unknown metric name/d' \
	    -e '/^mem\.util\.bufmem .* Unknown metric name/d' \
	    -e '/^name_cache\.enters .* Unknown metric name/d' \
	    -e '/^name_cache\.misses .* Unknown metric name/d' \
	    -e '/^name_cache\.removes .* Unknown metric name/d' \
	    -e '/^name_cache\.searches .* Unknown metric name/d' \
	    -e '/^network\.icmp .* Unknown metric name/d' \
	    -e '/^network\.igmp .* Unknown metric name/d' \
	    -e '/^network\.interface\.collisions .* Unknown metric name/d' \
	    -e '/^network\.ip .* Unknown metric name/d' \
	    -e '/^network\.tcp .* Unknown metric name/d' \
	    -e '/^network\.udp .* Unknown metric name/d' \
	    -e '/^network\.udplite .* Unknown metric name/d' \
	    -e '/^proc\..* Unknown metric name/d' \
	# freebsd
    fi
}

_filter_netbsd()
{
    if [ $PCP_PLATFORM != netbsd ]
    then
	cat
    else
	sed \
	    -e '/^disk\.all\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.all\.blkread .* Unknown metric name/d' \
	    -e '/^disk\.all\.blkwrite .* Unknown metric name/d' \
	    -e '/^disk\.dev\.avactive .* Unknown metric name/d' \
	    -e '/^hinv\.nnode .* Unknown metric name/d' \
	    -e '/^kernel\.all\.io\..* Unknown metric name/d' \
	    -e '/^kernel\.all\.kpreempt .* Unknown metric name/d' \
	    -e '/^kernel\.all\.kswitch .* Unknown metric name/d' \
	    -e '/^kernel\.all\.nusers .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysexec .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysfork .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysioctl .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysread .* Unknown metric name/d' \
	    -e '/^kernel\.all\.syswrite .* Unknown metric name/d' \
	    -e '/^kernel\.all\.uptime .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.pswitch .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.syscall .* Unknown metric name/d' \
	    -e '/^mem\.util\.bufmem .* Unknown metric name/d' \
	    -e '/^name_cache\.enters .* Unknown metric name/d' \
	    -e '/^name_cache\.misses .* Unknown metric name/d' \
	    -e '/^name_cache\.removes .* Unknown metric name/d' \
	    -e '/^name_cache\.searches .* Unknown metric name/d' \
	    -e '/^network\.icmp .* Unknown metric name/d' \
	    -e '/^network\.igmp .* Unknown metric name/d' \
	    -e '/^network\.interface\.collisions .* Unknown metric name/d' \
	    -e '/^network\.ip .* Unknown metric name/d' \
	    -e '/^network\.tcp .* Unknown metric name/d' \
	    -e '/^network\.udp .* Unknown metric name/d' \
	    -e '/^network\.udplite .* Unknown metric name/d' \
	    -e '/^proc\..* Unknown metric name/d' \
	# netbsd
    fi
}

_filter_openbsd()
{
    if [ $PCP_PLATFORM != openbsd ]
    then
	cat
    else
	sed \
	    -e '/^disk\.all\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.all\.blkread .* Unknown metric name/d' \
	    -e '/^disk\.all\.blkwrite .* Unknown metric name/d' \
	    -e '/^disk\.dev\.avactive .* Unknown metric name/d' \
	    -e '/^disk\.partitions\..* Unknown metric name/d' \
	    -e '/^filesys.* Unknown metric name/d' \
	    -e '/^hinv\.cpu\.clock .* Unknown metric name/d' \
	    -e '/^hinv\.nnode .* Unknown metric name/d' \
	    -e '/^kernel\.all\.boottime .* Unknown metric name/d' \
	    -e '/^kernel\.all\.cpu\.guest .* Unknown metric name/d' \
	    -e '/^kernel\.all\.cpu\.guest_nice .* Unknown metric name/d' \
	    -e '/^kernel\.all\.cpu\.irq\.soft .* Unknown metric name/d' \
	    -e '/^kernel\.all\.cpu\.steal .* Unknown metric name/d' \
	    -e '/^kernel\.all\.cpu\.wait\.total .* Unknown metric name/d' \
	    -e '/^kernel\.all\.io\..* Unknown metric name/d' \
	    -e '/^kernel\.all\.kpreempt .* Unknown metric name/d' \
	    -e '/^kernel\.all\.kswitch .* Unknown metric name/d' \
	    -e '/^kernel\.all\.nusers .* Unknown metric name/d' \
	    -e '/^kernel\.all\.pid_max .* Unknown metric name/d' \
	    -e '/^kernel\.all\.pressure\..* Unknown metric name/d' \
	    -e '/^kernel\.all\.pswitch .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysexec .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysfork .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysioctl .* Unknown metric name/d' \
	    -e '/^kernel\.all\.sysread .* Unknown metric name/d' \
	    -e '/^kernel\.all\.syswrite .* Unknown metric name/d' \
	    -e '/^kernel\.all\.uptime .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.cpu\.guest .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.cpu\.guest_nice .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.cpu\.irq\.soft .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.cpu\.steal .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.cpu\.wait\.total .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.pswitch .* Unknown metric name/d' \
	    -e '/^kernel\.percpu\.syscall .* Unknown metric name/d' \
	    -e '/^mem\.physmem .* Unknown metric name/d' \
	    -e '/^mem\.util\.bufmem .* Unknown metric name/d' \
	    -e '/^mem\.util\.shmem .* Unknown metric name/d' \
	    -e '/^mem\.util\.slabReclaimable .* Unknown metric name/d' \
	    -e '/^name_cache\.enters .* Unknown metric name/d' \
	    -e '/^name_cache\.misses .* Unknown metric name/d' \
	    -e '/^name_cache\.removes .* Unknown metric name/d' \
	    -e '/^name_cache\.searches .* Unknown metric name/d' \
	    -e '/^network\.all\.in\.bytes .* Unknown metric name/d' \
	    -e '/^network\.all\.in\.packets .* Unknown metric name/d' \
	    -e '/^network\.all\.out\.bytes .* Unknown metric name/d' \
	    -e '/^network\.all\.out\.packets .* Unknown metric name/d' \
	    -e '/^network\.icmp .* Unknown metric name/d' \
	    -e '/^network\.igmp .* Unknown metric name/d' \
	    -e '/^network\.interface\.collisions .* Unknown metric name/d' \
	    -e '/^network\.ip .* Unknown metric name/d' \
	    -e '/^network\.tcp .* Unknown metric name/d' \
	    -e '/^network\.tcp\.listenoverflows .* Unknown metric name/d' \
	    -e '/^network\.tcp\.reqqfulldocookies .* Unknown metric name/d' \
	    -e '/^network\.tcp\.reqqfulldrop .* Unknown metric name/d' \
	    -e '/^network\.udp .* Unknown metric name/d' \
	    -e '/^network\.udplite .* Unknown metric name/d' \
	    -e '/^proc\..* Unknown metric name/d' \
	    -e '/^zram\..* Unknown metric name/d' \
	# openbsd
    fi
}

_filter_linux()
{
    # pcp-atop uses metrics not supported on some kernels
    # proc.smaps.* metrics are not present for older kernels
    #
    if [ $PCP_PLATFORM != linux ]
    then
	cat
    else
	sed \
	    -e '/^network\.softnet.* Metric not supported/d' \
	    -e '/^nfsclient\.options.* Metric not supported/d' \
	    -e '/^proc\.namespaces.* Metric not supported/d' \
	    -e '/^proc\.psinfo\.cgroups.* No data available/d' \
	    -e '/^cgroup\..*\.container.* No data available/d' \
	    -e '/^proc\.id\.container.* No data available/d' \
	    -e '/^nfsclient\.options.* Unknown metric name/d' \
	    -e '/^perfevent\.hwcounters.* Unknown metric name/d' \
	    -e '/^proc\.smaps\..* Metric not supported/d' \
	# linux
    fi
}

# remove all known platform-dependencies from output
# (ideally leaving any genuine failures behind)
#
_filter()
{
    # these are always-on in older oss versions of pcp
    sed \
       -e '/^disk\.dev\.avg_disk\.active .* metric name/d' \
       -e '/^disk\.all\.avg_disk\.active .* metric name/d' \
       -e '/^disk\.dev\.bytes .* Unknown metric name/d' \
       -e '/^disk\.all\.bytes .* Unknown metric name/d' \
       -e '/^network\.igmp .* Unknown metric name/d' \
       -e '/^nfs.* Unknown metric name/d' \
       -e '/^rpc.* Unknown metric name/d' \
       -e '/^nfsclient.* Unknown metric name/d' \
       -e '/^elasticsearch\.search\.all.* Metric not supported/d' \
       -e '/^elasticsearch\.nodes\.jvm.* Metric not supported/d' \
       -e '/^elasticsearch\.nodes\.process.* Metric not supported/d' \
       -e '/^elasticsearch\.nodes\.indices\.cache.* Metric not supported/d' \
       -e '/^elasticsearch\.nodes\.indices\.docs\.num_docs .* not supported/d' \
       -e '/^oracle.* Information not currently available/d' \
       -e '/^swap\.in .* Metric not supported/d' \
       -e '/^swap\.out .* Metric not supported/d' \
    | _filter_linux \
    | _filter_solaris \
    | _filter_darwin \
    | _filter_freebsd \
    | _filter_netbsd \
    | _filter_openbsd
}

# real QA test starts here
pmlogconf $tmp.conf >$tmp.out 2>&1
cat $tmp.out >$seq.full

sed -e 's/:n:/:y:/' <$tmp.conf >$tmp.tmp
mv $tmp.tmp $tmp.conf

yes '' | pmlogconf $tmp.conf >$tmp.out
( echo && echo "== TMP.OUT" ) >>$seq.full
cat $tmp.out >>$seq.full
( echo && echo "== TMP.CONF") >>$seq.full
cat $tmp.conf >>$seq.full
( echo && echo "== FILTERED") >>$seq.full

sed -n <$tmp.conf \
    -e '/^[ 	]/{
s/^[ 	]*//
s/[ 	].*//
/^#/d
p
}' \
| LC_COLLATE=POSIX sort \
| uniq \
| tee -a $seq.full \
| xargs pmprobe $target \
| _filter \
| $PCP_AWK_PROG '
$2 >= 0		{ next }
		{ print }'

echo Silence is golden
# success, all done
exit
